﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;

namespace JasonTools
{
    public partial class MonthSlider : UserControl
    {
        #region Properties

        PhoneApplicationPage _Page = null;

        #endregion

        public MonthSlider()
        {
            InitializeComponent();
            InitMonthSlider();
        }

        public MonthSlider(PhoneApplicationPage page)
            : this()
        {
            _Page = page;
        }

        #region SelectedMonth Property

        public static readonly DependencyProperty SelectedMonthProperty =
            DependencyProperty.Register(
                "SelectedMonth",
                typeof(DateTime),
                typeof(MonthSlider),
                new PropertyMetadata(DateTime.MinValue,
                    SelectedMonthPropertyChanged));

        public DateTime SelectedMonth
        {
            get { return (DateTime)GetValue(SelectedMonthProperty); }
            set { SetValue(SelectedMonthProperty, value); }
        }

        private static void SelectedMonthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DateTime newValue = (DateTime)e.NewValue;
            DateTime oldValue = (DateTime)e.OldValue;

            MonthSlider slider = d as MonthSlider;
            slider.LayoutRootGrid.DataContext = newValue;

            slider.SetDataContext(newValue);

            if (slider.SelectedMonthChanged != null)
            {
                slider.SelectedMonthChanged(slider, newValue);
            }
        }

        public Grid LayoutRootGrid
        {
            get
            {
                return this.LayoutRoot;
            }
        }

        #endregion

        private void InitMonthSlider()
        {
            var states = (from stateGroup in (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(this.LayoutRoot)
                          where stateGroup.Name == "MonthSliderStateGroup"
                          select stateGroup).FirstOrDefault().States;

            var previousMonthState = from state in
                                         (Collection<VisualState>)states
                                     where state.Name == "VSTPreviousMonth"
                                     select state;
            previousMonthState.FirstOrDefault().Storyboard.Completed += new EventHandler(previousMonthState_Completed);

            var nextMonthState = from state in
                                     (Collection<VisualState>)states
                                 where state.Name == "VSTNextMonth"
                                 select state;
            nextMonthState.FirstOrDefault().Storyboard.Completed += new EventHandler(nextMonthState_Completed);

            //dateSilder
            SelectedMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

            //update UI
            SetDataContext(SelectedMonth);
        }

        void previousMonthState_Completed(object sender, EventArgs e)
        {
            this.SelectedMonth = this.SelectedMonth.AddMonths(-1);
            VisualStateManager.GoToState(this, "VSTCurrentMonth", false);
        }

        void nextMonthState_Completed(object sender, EventArgs e)
        {
            this.SelectedMonth = this.SelectedMonth.AddMonths(1);
            VisualStateManager.GoToState(this, "VSTCurrentMonth", false);
        }

        private void monthSlider_Flick(object sender, Microsoft.Phone.Controls.FlickGestureEventArgs e)
        {
            if (e.HorizontalVelocity > 0)
            {
                VisualStateManager.GoToState(this, "VSTPreviousMonth", true);
            }
            else if (e.HorizontalVelocity < 0)
            {
                VisualStateManager.GoToState(this, "VSTNextMonth", true);
            }
        }

        private void SetDataContext(DateTime month)
        {
            if (month >= DateTime.MinValue.AddMonths(2))
            {
                this.twoMonthsAgo.DataContext = month.AddMonths(-2);
            }
            if (month >= DateTime.MinValue.AddMonths(1))
            {
                this.previousMonth.DataContext = month.AddMonths(-1);
            }
            this.currentMonth.DataContext = month;
            this.nextMonth.DataContext = month.AddMonths(1);
            this.twoMonthsLater.DataContext = month.AddMonths(2);
        }

        #region Event

        public event SelectedMonthChangedEventHandler SelectedMonthChanged;

        #endregion

        private void previousArea_Tap(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "VSTPreviousMonth", true);
        }

        private void nextArea_Tap(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "VSTNextMonth", true);
        }

        #region Popup

        PopupCotainer _PopupCotainer = null;

        #endregion

        #region Show & Hide

        public void Show()
        {
            if (_Page==null)
            {
                throw new ArgumentNullException("_Page", "the calling page shoul be given before show the MonthSlider as a popup.");
            }

            if (_PopupCotainer == null)
            {
                _PopupCotainer = new PopupCotainer(_Page);
            }

            _PopupCotainer.Show(this);
        }

        public void Hide()
        {
            this.CloseMeAsPopup();
        }

        #endregion
    }

    public delegate void SelectedMonthChangedEventHandler(object sender, DateTime e);

}
